import request from '@/request/http'
import router from '@/router/index'
import { ElMessage } from "element-plus";
import Cookies from 'js-cookie'

const tokenKey = process.env.VUE_APP_TOKEN 
/**
 * 登录
 * @param {登录请求参数} params 
 * @returns 
 */
export function doLogin(params) {
    return request({
        url: '/sys/user/login',
        method: 'post',
        data: params
    }).then((data) => {
        const authorization = data.headers.getAuthorization()
        localStorage.setItem(tokenKey, authorization)
        router.push('/dashboard')
    }).catch(err => err)
}

/**
 * 登出
 * @returns 
 */
 export function doLogout() {
    return request({
        url: '/sys/user/logout',
        method: 'get'
    }).then(() => {
        localStorage.removeItem(tokenKey)
        Cookies.remove(tokenKey)
        router.replace('/login')
    }).catch(err => err)
}

/**
 * 获取用户信息
 * @returns 
 */
 export function doGetUser() {
    return request({
        url: '/sys/user/get',
        method: 'post'
    }).then(rsp => Promise.resolve(rsp)).catch(err => err)
}